package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.type.IntegerTypeHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mysql.fabric.Response;

import entity.Comment;
import entity.Users;
import entity.Video;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import service.CommentService;
import service.ReplyService;
import service.UsersService;
import util.PageUtil;

@Controller
@RequestMapping("/comment")
public class CommentController {

	@Autowired
	CommentService commentService;
	@Autowired
	UsersService usersService;
	@Autowired
	Users user;
	@Autowired
	Video video;
	@Autowired
	Comment comment;
	@Autowired
	ReplyService replyService;
	
	//带播单的分页评论内容
	@RequestMapping("/havelist")
	public void comment_havelist(int pageIndex, int listId, HttpServletResponse response) {
		
		PageUtil<Comment> commentPage = commentService.findByListId(listId, pageIndex, 5);
		System.out.println(commentPage.getList().size());

		//将json所需的包放到lib里面,将list转换为json
				//如果只是一个对象用JSONObject，如果是集合用JSONArray
		JSONObject json=JSONObject.fromObject(commentPage);
        
        //响应前台页面
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/json");
        PrintWriter out;
        try {
			out = response.getWriter();
			out.println(json);//打印json，为了前台页面获取
			out.flush();//刷新流
			out.close();//关闭流
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	//不带播单的分页评论内容
	@RequestMapping("/notlist")
	public void comment_notlist(int pageIndex, int videoId, HttpServletResponse response) {
		System.out.println(videoId + " -- " + pageIndex);
		PageUtil<Comment> commentPage = commentService.fingByVideoId(videoId, pageIndex, 5);
		System.out.println(commentPage.getList().size());

		//将json所需的包放到lib里面,将list转换为json
				//如果只是一个对象用JSONObject，如果是集合用JSONArray
		JSONObject json=JSONObject.fromObject(commentPage);
        
        //响应前台页面
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/json");
        PrintWriter out;
        try {
			out = response.getWriter();
			out.println(json);//打印json，为了前台页面获取
			out.flush();//刷新流
			out.close();//关闭流
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	//添加一条留言
	@RequestMapping("/add")
	public void add(int videoId, String detail, HttpServletRequest request, HttpServletResponse response) {
	//	videoId = 1;
		if(request.getSession().getAttribute("username") == null) {
			try {
				response.getWriter().write("nologin");
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			String username = (String) request.getSession().getAttribute("username");
			//	String username = "123";
			user = usersService.findByUserName(username);
			video.setId(videoId);
			comment.setCommTime(new Date());
			comment.setDetail(detail);
			comment.setUser(user);
			comment.setVideo(video);
			try {
				System.out.println(1);
				commentService.add(comment);
				response.getWriter().write("success");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	//封禁一条留言或者回复
	@RequestMapping("/tip_off") 
	public void tip_off(String type, String id, HttpServletRequest request ,HttpServletResponse response) {
		System.out.println(type + id);
		Map<String, String> map = new HashMap<String, String>();
		
		try {
			if(request.getSession().getAttribute("username") == null) {  //判断举报用户是否登录
				map.put("login", "error");
			} else {
				int id_c = Integer.valueOf(id);
				if( type.equals("comment")) {                 //判断举报的信息是何种
					commentService.updateCommState(1, id_c);
					map.put("login", "success");
					map.put("msg", "留言举报成功，等待审核");
					
				}else if( type.equals("reply") ) {
					replyService.updateReplyState(1, id_c);
					map.put("login", "success");
					map.put("msg", "回复举报成功，等待审核");
				}
			}
			
			JSONObject json=JSONObject.fromObject(map);
			//响应前台页面
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/json");
			PrintWriter out=response.getWriter();
			out.println(json);//打印json，为了前台页面获取
			out.flush();//刷新流
			out.close();//关闭流
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//更新留言或者回复的状态
	@RequestMapping("/update_state") 
	public void updateState(String id, String type, String state, HttpServletResponse response) {
		int state_c = Integer.valueOf(state);   //获取状态
		int id_c = Integer.valueOf(id);   //获取编号
		List list = null;
		try {
			if(type.equals("comment")) {
				commentService.updateCommState(state_c, id_c);  //更新留言状态
				list = commentService.findByState(1);
			} else if(type.equals("reply")){
				replyService.updateReplyState(state_c, id_c);   //更新回复状态
				list = replyService.findByState(1);
			}
			
			JSONArray json=JSONArray.fromObject(list);
			//响应前台页面
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/json");
			PrintWriter	out = response.getWriter();
			out.println(json);//打印json，为了前台页面获取
			out.flush();//刷新流
			out.close();//关闭流
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//更新审核信息
	@RequestMapping("/chang_msg") 
	public void updateState(String type, HttpServletResponse response) {
		List list = null;
		try {
			if(type.equals("comment")) {
				list = commentService.findByState(1);   //更新留言状态
			} else {
				list = replyService.findByState(1);    //更新回复状态
			}
			
			JSONArray json=JSONArray.fromObject(list);
			//响应前台页面
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/json");
			PrintWriter	out = response.getWriter();
			out.println(json);//打印json，为了前台页面获取
			out.flush();//刷新流
			out.close();//关闭流
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
}
